﻿using System;
using System.Threading.Tasks;
using DjangoRESTFrameworkDAL.Model;
using Newtonsoft.Json;

namespace DjangoRESTFrameworkDAL.REST
{
    public interface IGetPage<T>
    {
        string Url { get; }
    }


    public static class GetPageEx
    {
        /// <summary>
        ///     返回列表对象
        /// </summary>
        /// <typeparam name="T">序列化数据模型类型</typeparam>
        /// <param name="dal">DAL实例化对象</param>
        /// <returns>返回实例列表</returns>
        public static async Task<LargeResultsSetPagination<T>> GetPageAsync<T>(this IGetPage<T> dal)
        {
            if (dal.Url == null)
            {
                throw new ArgumentNullException(nameof(dal.Url));
            }
            var response = await RestHttpClient.Instance.GetWithPolicyAsync(dal.Url);
            var responseBody = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<LargeResultsSetPagination<T>>(responseBody);
        }

        /// <summary>
        /// 获取当前页面
        /// </summary>
        /// <typeparam name="T">序列化数据模型类型</typeparam>
        /// <param name="page">当前页</param>
        /// <returns></returns>
        public static async Task<LargeResultsSetPagination<T>> GetPageAsync<T>(this LargeResultsSetPagination<T> page)
        {
            if (page.Url == null)
            {
                throw new ArgumentNullException(nameof(page.Url));
            }
            var response = await RestHttpClient.Instance.GetWithPolicyAsync(page.Url);
            var responseBody = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<LargeResultsSetPagination<T>>(responseBody);
        }

        /// <summary>
        /// 获取前一页
        /// </summary>
        /// <typeparam name="T">序列化数据模型类型</typeparam>
        /// <param name="page">当前页</param>
        /// <returns></returns>
        public static async Task<LargeResultsSetPagination<T>> GetPreviousPageAsync<T>(this LargeResultsSetPagination<T> page)
        {
            if (page.PreviousUrl == null)
            {
                throw new ArgumentNullException(nameof(page.PreviousUrl));
            }
            var response = await RestHttpClient.Instance.GetWithPolicyAsync(page.PreviousUrl);
            var responseBody = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<LargeResultsSetPagination<T>>(responseBody);
        }

        /// <summary>
        /// 获取下一页
        /// </summary>
        /// <typeparam name="T">序列化数据模型类型</typeparam>
        /// <param name="page">当前页</param>
        /// <returns></returns>
        public static async Task<LargeResultsSetPagination<T>> GetNextPageAsync<T>(this LargeResultsSetPagination<T> page)
        {
            if (page.NextUrl == null)
            {
                throw new ArgumentNullException(nameof(page.NextUrl));
            }
            var response = await RestHttpClient.Instance.GetWithPolicyAsync(page.NextUrl);
            var responseBody = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<LargeResultsSetPagination<T>>(responseBody);
        }
    }
}
